package com.ywgt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ywgt.system.pojo.ProductCategory;
import com.ywgt.system.mapper.ProductCategoryMapper;
import com.ywgt.system.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Override
    public List<ProductCategory> getMenuTree() {
        return productCategoryMapper.getMenuTree();
    }

    @Override
    public List<ProductCategory> getParentMenu() {
        return productCategoryMapper.getParentMenu();
    }

    @Override
    public void delete(Long id) {
        LambdaQueryWrapper<ProductCategory> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCategory::getPid,id);
        List<Long> idList=new ArrayList<>();
        List<ProductCategory> categoryList = productCategoryMapper.selectList(queryWrapper);
        idList.add(id);
        if (categoryList.size()>0) {
            List<Long> ids = categoryList.stream().map(ProductCategory::getId).collect(Collectors.toList());
            idList.addAll(ids);
            LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(ProductCategory::getPid, ids);
            List<ProductCategory> categories = productCategoryMapper.selectList(wrapper);
            if (categories.size() > 0) {
                 idList.addAll(categories.stream().map(ProductCategory::getId).collect(Collectors.toList()));
            }
        }
        productCategoryMapper.deleteBatchIds(idList);
    }
}
